package kz.edu.sdu.itech.pcs.server;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Locale;

import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;

public class Main {

   static Logger log = Logger.getLogger(Main.class);
   static Server server;

   public static void main(String[] args) {
      BasicConfigurator.configure();
      Locale.setDefault(Locale.US);
      try {
         server = new Server();
         server.init();
         BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
         while (!AppProps.DONE) {
            try {
               String q = in.readLine();
               if (q.equals("q")) {
                  log.debug("CLOSING SERVER");
                  server.close();
                  AppProps.DONE = true;
               } else if (q.startsWith("start")) {
                  String st[] = q.split(" ");
                  server.startSthOn(st[1]);
               } else if (q.startsWith("stop")) {
                  String st[] = q.split(" ");
                  server.stopSthOn(st[1]);
               } else if (q.startsWith("ls")) {
                  server.list();
               } else if (q.startsWith("job")) {
                  String st[] = q.split(" ");
                  server.setupJob(st[1], Integer.parseInt(st[2]), Integer.parseInt(st[3]));
                  server.startJob();
               }
            } catch (Exception e) {
               e.printStackTrace();
            }
         }
      } catch (Exception e) {
         e.printStackTrace();
      }

   }
}
